﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Dovetailendmillsolid
    {
        public static void _Dovetailendmillsolid(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 3; //放大系数

            double dc = Convert.ToDouble(data["dc"]) * scale;
            double kapr = Convert.ToDouble(data["kapr"]) * scale;
            double lh = Convert.ToDouble(data["lh"]) * scale;
            double oal = Convert.ToDouble(data["oal"]) * scale;
            double dn = Convert.ToDouble(data["dn"]) * scale;
            double dconms = Convert.ToDouble(data["dconms"]) * scale;



            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X - oal - 4, v1.Y);
            Vector2 v3 = new Vector2(v1.X - 2, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + dc / 2);
            Vector2 v5 = new Vector2(v4.X, v4.Y - dc);
            Vector2 v6 = fun.Polar(v5, (90 + 180 - kapr) * pi / 180, lh / Math.Sin((180 - kapr) * pi / 180));
            Vector2 v7 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v8=fun.Polar (v5, (90 + 180 - kapr) * pi / 180, lh / 1.258 / Math.Sin((180 - kapr) * pi / 180));
            Vector2 v9 = new Vector2(v8.X, v8.Y * -1);
            Vector2 v10 = new Vector2(v9.X, v9.Y - dc / 10.526);
            Vector2 v11 = new Vector2(v4.X, v4.Y - dc / 10.526);
            Vector2 v12 = new Vector2(v10.X, v10.Y - dc / 16.949);
            Vector2 v13 = fun.Polar(v12, 27 * pi / 180, lh / 1.379);
            Vector2 v14 = new Vector2(v11.X, v11.Y - dc / 26.316);
            Vector2 v15 = new Vector2(v12.X, v12.Y - dc / 16);
            Vector2 v16 = new Vector2(v14.X, v14.Y - dc / 7.22);
            Vector2 v17 = new Vector2(v15.X, v15.Y - dc / 22.222);
            Vector2 v18 = fun.Polar(v17, 13 * pi / 180, lh / 1.473);
            Vector2 v19 = new Vector2(v16.X, v16.Y - dc / 13.605);
            Vector2 v20 = new Vector2(v17.X, v17.Y * -1);
            Vector2 v21 = new Vector2(v18.X, v18.Y * -1);
            Vector2 v22 = new Vector2(v19.X, v19.Y * -1);
            Vector2 v23 = new Vector2(v15.X, v15.Y * -1);
            Vector2 v24 = new Vector2(v16.X, v16.Y * -1);
            Vector2 v25 = new Vector2(v12.X, v12.Y * -1);
            Vector2 v26 = new Vector2(v13.X, v13.Y * -1);
            Vector2 v27 = new Vector2(v14.X, v14.Y * -1);
            Vector2 v28 = new Vector2(v10.X, v10.Y * -1);
            Vector2 v29 = new Vector2(v11.X, v11.Y * -1);
            Vector2 v30 = new Vector2(v7.X, v3.Y + dn / 2);
            Vector2 v31 = new Vector2(v30.X - oal / 4.846, v30.Y);
            Vector2 v32 = new Vector2(v30.X, v30.Y * -1);
            Vector2 v33 = new Vector2(v31.X, v32.Y);
            Vector2 v34 = new Vector2(v3.X - oal, v3.Y + dconms / 2.211);
            Vector2 v35 = new Vector2(v34.X + 0.8, v3.Y + dconms / 2);
            Vector2 v36 = new Vector2(v31.X - oal / 29.577, v35.Y);
            Vector2 v37 = new Vector2(v36.X, v36.Y * -1);
            Vector2 v38 = new Vector2(v35.X, v35.Y * -1);
            Vector2 v39 = new Vector2(v34.X, v34.Y * -1);
            Vector2 v40 = new Vector2(v34.X, v3.Y);
            Vector2 v41 = new Vector2(v3.X - oal / 2.172, v3.Y);
            Vector2 v42 = new Vector2(v41.X, v41.Y + dconms / 2 + 2);
            Vector2 v43 = new Vector2(v42.X, v42.Y * -1);
            Vector2 v44 = fun.Polar(v5, (90 + 180 - kapr) * pi / 180, lh / 1.6 / Math.Sin((180 - kapr) * pi / 180));
            Vector2 v45 = new Vector2(v44.X, v44.Y * -1);



            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            fun.PolyFill(dxf, line2, 0.25, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v8));
            line3.Vertexes.Add(new LwPolylineVertex(v9));
            fun.PolyFill(dxf, line3, 0.25, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v11));
            line4.Vertexes.Add(new LwPolylineVertex(v10));
            line4.Vertexes.Add(new LwPolylineVertex(v12));
            line4.Vertexes.Add(new LwPolylineVertex(v13));
            line4.Vertexes.Add(new LwPolylineVertex(v14));
            line4.Vertexes[3].Bulge = Math.Tan(54 * pi / 180 / 4);
            fun.PolyFill(dxf, line4, 0.25, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v16));
            line5.Vertexes.Add(new LwPolylineVertex(v15));
            line5.Vertexes.Add(new LwPolylineVertex(v17));
            line5.Vertexes.Add(new LwPolylineVertex(v18));
            line5.Vertexes.Add(new LwPolylineVertex(v19));
            line5.Vertexes[3].Bulge = Math.Tan(70 * pi / 180 / 4);
            fun.PolyFill(dxf, line5, 0.25, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v24));
            line6.Vertexes.Add(new LwPolylineVertex(v23));
            line6.Vertexes.Add(new LwPolylineVertex(v20));
            line6.Vertexes.Add(new LwPolylineVertex(v21));
            line6.Vertexes.Add(new LwPolylineVertex(v22));
            line6.Vertexes[3].Bulge = Math.Tan(-54 * pi / 180 / 4);
            fun.PolyFill(dxf, line6, 0.25, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v29));
            line7.Vertexes.Add(new LwPolylineVertex(v28));
            line7.Vertexes.Add(new LwPolylineVertex(v25));
            line7.Vertexes.Add(new LwPolylineVertex(v26));
            line7.Vertexes.Add(new LwPolylineVertex(v27));
            line7.Vertexes[3].Bulge = Math.Tan(-70 * pi / 180 / 4);
            fun.PolyFill(dxf, line7, 0.25, contour);

            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v30));
            line8.Vertexes.Add(new LwPolylineVertex(v31));
            line8.Vertexes.Add(new LwPolylineVertex(v36));
            line8.Vertexes.Add(new LwPolylineVertex(v35));
            line8.Vertexes.Add(new LwPolylineVertex(v34));
            line8.Vertexes.Add(new LwPolylineVertex(v39));
            line8.Vertexes.Add(new LwPolylineVertex(v38));
            line8.Vertexes.Add(new LwPolylineVertex(v37));
            line8.Vertexes.Add(new LwPolylineVertex(v33));
            line8.Vertexes.Add(new LwPolylineVertex(v32));
            line8.Vertexes.Add(new LwPolylineVertex(v33));
            line8.Vertexes.Add(new LwPolylineVertex(v31));
            line8.Vertexes.Add(new LwPolylineVertex(v36));
            line8.Vertexes.Add(new LwPolylineVertex(v37));
            line8.Vertexes.Add(new LwPolylineVertex(v38));
            line8.Vertexes.Add(new LwPolylineVertex(v35));
            fun.PolyFill(dxf, line8, 0.25, contour);

            Line line9 = new Line(v42, v43);
            line9.Layer = centre;
            line9.Linetype = Linetype.DashDot;
            line9.LinetypeScale = 3;
            dxf.AddEntity(line9);

            fun.drawcircle(dxf, v5, 2, 0.01, dimensioning);


            //坐标系
            fun.Axis(dxf, v40, -90, 180, "", "", "PCS", 1, 2, 0);

            fun.Axis(dxf, v41, -90, 180, "", "", "MCS", 1, 2, 0);

            fun.Axis(dxf, v3, -90, 180, "", "", "CIP", 1, 2, 0);




            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;



            fun.dimrot1(dxf, v34, v4, 0, oal / 2, 1, height, arrowsize, exline, "OAL,LF", "", "");

            fun.dimrot1(dxf, v42, v4, 0, oal / 3, 1, height, arrowsize, exline, "LPR,LTA", "", "");

            fun.dimrot1(dxf, v35, v38, 90, dconms, 1, height, arrowsize, exline, "DCONMS", "", "");

            fun.dimrot1(dxf, v31, v33, -90, dconms, 1, height, arrowsize, exline, "DN", "", "");

            fun.dimrot1(dxf, v39, v37, 180, oal / 3, 1, height, arrowsize, exline, "LS", "", "");

            fun.dimrot1(dxf, v6, v5, 180, oal / 2, 1, height, arrowsize, exline, "LH", "", "");

            fun.dimrot1(dxf, v44, v5, 180, oal / 3 - (v37.Y - (v44.Y + v5.Y) / 2), 1, height, arrowsize, exline, "APMX", "", "");

            fun.dimrot1(dxf, v7, v6, -90, (v3.X - v7.X) * 2, 1, height, arrowsize, exline, "BD", "", "");

            fun.dimrot1(dxf, v44, v45, -90, (v3.X - v7.X) * 2.5, 1, height, arrowsize, exline, "DCN", "", "");

            fun.dimrot1(dxf, v4, v5, -90, (v3.X - v7.X) * 2.5, 1, height, arrowsize, exline, "DC", "", "");

            fun.jdbz2(dxf, new Line(v4, fun.Polar(v4, 90 * pi / 180, 0.1)), new Line(v4, fun.Polar(v4, (90 + kapr) * pi / 180, 0.1)), (v5.X - v6.X), height, arrowsize, exline, "KAPR", "", "");

            fun.jt(dxf, new Vector2(v4.X + oal / 3.864, v4.Y + dc / 0.929), -90, 9, height, "FDP");

            fun.text1(dxf, new Vector2(v5.X + oal / 15.818, v5.Y - dc / 3.223), 90, "CRP");


            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}
